package edu.whu.rose.clock.ssql.graphindex;

import com.sleepycat.je.Cursor;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.LockMode;
import com.sleepycat.je.OperationStatus;

import edu.whu.rose.clock.setup.IndexEnvironment;
import edu.whu.rose.clock.ssql.binding.GraphIndexItemBinding;
import edu.whu.rose.clock.ssql.binding.SequentialIntArrayBinding;
import edu.whu.rose.clock.ssql.graph.SubGraphDB;
import edu.whu.rose.clock.ssql.index.VertexIndexDB;

public class GraphIndexTest {

	private GraphIndexDB gidb;
	
	public GraphIndexTest(GraphIndexDB gidb) {
		this.gidb = gidb;
	}
	
	public static void main(String[] args) {
		
		IndexEnvironment ie2 = new IndexEnvironment("dbEnv_graphindex");
		if (!ie2.openEnv()) {
			System.exit(1);
		}
		GraphIndexDB gidb = new GraphIndexDB(ie2.getEnvironment());
		if (!gidb.openDB()) {
			return;
		}
		
		try {
			Cursor cursor = gidb.getCursor();
			DatabaseEntry key = new DatabaseEntry();
			DatabaseEntry value = new DatabaseEntry();
			int i = 0;
			while (cursor.getNext(key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
				i++;
			}
			System.out.println("Total number of keywords: " + i);
			cursor.close();
		}
		catch (DatabaseException ex) {
			ex.printStackTrace();
		}
		
		gidb.closeDB();
	}
	
	public void createGraphIndex(VertexIndexDB vidb, SubGraphDB sgdb) {
		try {
			DatabaseEntry key = new DatabaseEntry();
			DatabaseEntry value = new DatabaseEntry();
			
			Cursor cursor = vidb.getCursor();
			
			int counter = 0;
			while (cursor.getNext(key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
				SequentialIntArrayBinding siab = new SequentialIntArrayBinding();
				int[] vertexes = (int[])siab.entryToObject(value);
				
//				HashMap<Integer, String> item = new HashMap<Integer, String>();
				GraphIndexItem gii = new GraphIndexItem();
				for (int i = 0; i < vertexes.length; i++) {
					int[] temp = sgdb.getFromInvertedList(vertexes[i]);
					if (temp != null) {
						gii.add(temp, vertexes[i]);
					}
					else {
						System.err.println("Error: found a vertex " + vertexes[i] + " dont belong to any subgraph");
					}
//					for (int j = 0; j < temp.length; j++) {
//						Integer graph = new Integer(temp[j]);
//						if (item.containsKey(graph)) {
//							String str = item.get(graph);
//							str += " " + vertexes[i];
//						}
//						else {
//							item.put(graph, "" + vertexes[i]);
//						}
//					}
				}
				
				DatabaseEntry list = new DatabaseEntry();
				GraphIndexItemBinding giib = new GraphIndexItemBinding();
				giib.objectToEntry(gii, list);
				
				gidb.put(key, list);
				
				counter++;
				if (counter % 100 == 0)
					System.out.println("Num: " + counter);
			}
			cursor.close();
			System.out.println("Over");
		}
		catch (DatabaseException ex) {
			ex.printStackTrace();
		}
	}
	
//	public void print(String keyword) {
//		GraphIndexItem2 gii = gidb.search2(keyword);
//		for (int i = 0; i < gii.getGraphsLength(); i++) {
//			System.out.print(gii.getGraph(i) + "(");
//			for (int j = 0; j < gii.getVertexGroupLength(i); j++) {
//				System.out.print(gii.getVertex(i, j) + " ");
//			}
//			System.out.println("), ");
//		}
//	}

}
